package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.zm.cg.domain.ZMCGContactList;


public class ServiceZMCG2001 extends ServiceEPBase {

	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:26 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false, UserSession.getInSessionProperty("miningCode").toString()));
		return inInfo;
	}

	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:44 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		if(inInfo.getBlock(EiConstant.queryBlock) == null){
			StaticTools.setQueryParameters(inInfo, "fdEndDate", StaticTools.getDate() + " 23:59:59", true);
		}
		else {
			String endTime = inInfo.getBlock(EiConstant.queryBlock).getCellStr(0, "fdEndDate");
			if(null != endTime && !"".equals(endTime)) {
				if(endTime.length() <= 10) {
					StaticTools.setQueryParameters(inInfo, "fdEndDate", endTime + " 23:59:59", true);
				}
			}
			String fdTitle = inInfo.getBlock(EiConstant.queryBlock).getCellStr(0, "fdTitle");
			if(null != fdTitle && ("").equals(fdTitle)){
				StaticTools.setQueryParameters(inInfo, "fdTitle", fdTitle, true);//添加查询条件 
			}	
		}
		
		String fdSendPerson = UserSession.getInSessionProperty("UserCname") == null ? "" : UserSession.getInSessionProperty("UserCname").toString();
		String fdContactlistId = inInfo.getString("fdContactlistId");
		String fdTitle = inInfo.getString("fdTitle");
		String fdContent = inInfo.getString("fdContent");
		String fdSendDepartId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//发送部门
		StaticTools.setQueryParameters(inInfo, "fdSendDepartId", fdSendDepartId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdSendPerson", fdSendPerson, true);
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		
		EiInfo outInfo = super.query(inInfo, "ZMCGContactList.query", new ZMCGContactList());
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.set("fdSendPerson", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdSendTime", StaticTools.getDateTime());
		outInfo.set("fdContactlistId", fdContactlistId);
		outInfo.set("fdTitle", fdTitle);
		outInfo.set("fdContent", fdContent);
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:59 张志芳
	 *    2011-3-18下午 15:52:30 张小飞  添加编号，名称的重复性校验
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGContactList _dom = new ZMCGContactList();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String fdStatus = "草拟";
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String fdSendDepartId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//发送部门
			String fdSendPersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//发送人ID
			String fdSendPerson = (String) UserSession.getInSessionProperty("UserCname");
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_BUSINESSFILTER"));
			_dom.setFdSendDepartId(fdSendDepartId);
		    _dom.setFdSendPersonId(fdSendPersonId);
		    _dom.setFdSendTime(StaticTools.getDateTime());
		    _dom.setFdSendPerson(fdSendPerson);
		    _dom.setFdStatus(fdStatus);
		    _dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
		    this.dao.insert("ZMCGContactList.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:14 张志芳
	 *    2011-3-18下午 15:52:30 张小飞  添加编号，名称的重复性校验
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGContactList _dom = new ZMCGContactList();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			String fdSendDepartId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//发送部门
			String fdSendPersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//发送人ID
			String fdSendPerson = (String) UserSession.getInSessionProperty("UserCname");
			_dom.setFdSendDepartId(fdSendDepartId);
		    _dom.setFdSendPersonId(fdSendPersonId);
		    _dom.setFdSendTime(StaticTools.getDateTime());
		    _dom.setFdSendPerson(fdSendPerson);
		    this.dao.update("ZMCGContactList.update",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能修改草拟的联系单!"); 
				return out;
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * 发送联系单
	 * <p>Function: sendContact</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-7上午08:01:18 严洪浩
	 * </p>
	 *
	 */
	public EiInfo sendContact(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGContactList _dom = new ZMCGContactList();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String>	params = new HashMap<String, String>();
		int i = 0;
		String fdSendPerson = (String) UserSession.getInSessionProperty("UserCname");
		String fdSendPersonId = (String) UserSession.getUserId();
		String fdSendDepartId = (String) UserSession.getInSessionProperty("orgCode");
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdContactlistId", _dom.getFdKeyId());
			boolean validate = this.validate1(_dom, "ZMCGContactReceive.validate", params, "delete");
			if(!validate) { 
			out = this.query(inInfo);
			out.setMsg("该通知未存在明细信息，不可发送！");
			return out; 
			}else{				
			_dom.setFdSendTime(StaticTools.getDateTime());
			_dom.setFdSendPerson(fdSendPerson);
			_dom.setFdSendPersonId(fdSendPersonId);
			_dom.setFdSendDepartId(fdSendDepartId);
			 this.dao.update("ZMCGContactList.modifyFeedback",_dom);
			}
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:29 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGContactList _dom = new ZMCGContactList();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			this.dao.delete( "ZMCGContactReceive.delete", _dom);
			this.dao.delete("ZMCGContactList.delete", _dom);
			}else{				
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能删除草拟的联系单!"); 
				return out;				
		}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	private boolean validate(ZMCGContactList _dom){
		String fdStatus = _dom.getFdStatus();
			if("草拟".equals(fdStatus)){
				return true; 
			}else
				return false;
	}
	
	/**
	 * <p>Function: validate</p>
	 * <p>Description: 对主表记录上报前的后台校验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:40:23张坤
	 * </p>
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate1(ZMCGContactList _dom,String sqlName, Map<String, String> params, String type) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0 ) {
			return true;
		}		
		return false;
	}
	
	/**
	 * 查询附件
	 * <p>Function: queryFiles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30上午10:38:30 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryFiles(EiInfo inInfo) {
		String keyId = inInfo.getString("fdKeyId");
		return Documents.getFilesInfo("filesresult", keyId);
	}
}
